// pages/homepage/dietinput/dietinput.js
var app=getApp()
Page({
  data: {
    list : ''
  },

  onLoad:function(e){
    //console.log('hahaha:'+e.list);
    //fix:根据list设置各个输入框的值
    //var str="食物编号			:编号\n食物名			:食物名\n碳水化合物含量		:碳水\n纤维素含量			:纤维\n含糖量			:糖"
    var message=e.list.split("\n");
    var tempmes=new Array();
    for(var i=0;i<message.length;i++) {
      tempmes[i]=message[i].split(":");
      console.log(tempmes[i][1]);
    }
    this.setData({
      FoodNo:tempmes[0][1],
      FoodName:tempmes[1][1],
      CarboContent:tempmes[2][1],
      CellContent:tempmes[3][1],
      SugarContent:tempmes[4][1],
    });
  },

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var objDate = e.detail.value;
    if (objDate.id && objDate.name && objDate.carbohydrate && objDate.cellulose && objDate.sugarcontent) {
      wx.setStorage({
        key: 'id',
        data: objDate.id,
      })
      wx.setStorage({
        key: 'name',
        data: objDate.name,
      })
      wx.setStorage({
        key: 'carbohydrate',
        data: objDate.carbohydrate,
      })
      wx.setStorage({
        key: 'cellulose',
        data: objDate.cellulose,
      })
      wx.setStorage({
        key: 'sugarcontent',
        data: objDate.sugarcontent,
      })

      wx.request({
        url: app.globalData.url + '/API/dietinput',
        data: {
          'id': e.detail.value.id,
          'name': e.detail.value.name,
          'carbohydrate': e.detail.value.carbohydrate,
          'cellulose': e.detail.value.cellulose,
          'sugarcontent': e.detail.value.sugarcontent
        },
        method: 'get',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
        }
      })
      wx.showModal({
        title: '提示',
        content: '上传成功',
        showCancel: false
      })
      
      wx.redirectTo({
        url: '../../../homepage/homepage',
      })
    }
  },

  formReset: function () {
    console.log('form发生了reset事件')
  },
  onShareAppMessage: function () {
  }
})
